<?php
namespace app\admin\model;

use app\common\model\WorkerDepartment as WDM;

class WorkerDepartment extends WDM
{
    protected $append = ['nodes'];

    public function setNodeIndexsAttr($value)
    {
        return implode('|', $value);
    }

    public function getNodeIndexsAttr($value, $data)
    {
        return explode('|', $value);
    }

    public function getNodesAttr($value, $data)
    {
        static $nodes = [];

        if ( empty($nodes) ) {
            $listNodes = list_system_menu();
            foreach($listNodes as $menus) {
                if (isset($menus['son'])) {
                    foreach($menus['son'] as $son) {
                        $nodes[$son['id']] = $son['title'];
                    }
                }
            }
        }

        if ( isset($data['node_indexs']) ) {
            $nodeIndexs = explode('|', $data['node_indexs']);
            $nodeTotal = count($nodeIndexs);
            $nodeStr = [];
            for($i=0; $i < $nodeTotal; $i++) {
                if ( isset($nodes[$nodeIndexs[$i]]) ) {
                    $nodeStr[] = $nodes[$nodeIndexs[$i]];
                }
            }
        }

        if (!empty($nodeStr)) {
            return implode(' | ',$nodeStr);
        }
    }

    public function scopeWithKeyword($query, $keyword)
    {
        if ( !empty($keyword) ) {
            $query->where('name', 'LIKE', '%'.$keyword.'%');
        }
    }
}
